#include "tabledelegate.h"
#include <QTimeEdit>
#include <QDoubleSpinBox>
#include <QMessageBox>
TableDelegate::TableDelegate(QWidget *parent):QStyledItemDelegate(parent)
{

}
QWidget* TableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    if (index.column()==0){
        if (index.row()==0){
            return nullptr;
        }
        QTimeEdit* timeEdit=new QTimeEdit(parent);
        timeEdit->setGeometry(option.rect);
        return timeEdit;
    }else if (index.column()==1){
        QDoubleSpinBox* loadSpin=new QDoubleSpinBox(parent);
        loadSpin->setMinimum(0);
        loadSpin->setGeometry(option.rect);
        return loadSpin;
    }
    return nullptr;
}
void TableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
    if (index.column()==0){
        if (index.row()==0){
            return;
        }
        QTimeEdit* timeEdit=static_cast<QTimeEdit*>(editor);
        QTime time=timeEdit->time();
        model->setData(index,time);
    }else if (index.column()==1){
        model->setData(index,static_cast<QDoubleSpinBox*>(editor)->value());
    }
}
void TableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
    if (index.column()==0){
        if (index.row()==0){
            return;
        }
        QTime time=index.data(Qt::EditRole).toTime();
        static_cast<QTimeEdit*>(editor)->setTime(time);
    }else if (index.column()==1){
        double load=index.data(Qt::EditRole).toDouble();
        auto spin=static_cast<QDoubleSpinBox*>(editor);
        spin->setValue(load);
    }
}
